package org.hengguan.api.contest.module.contest.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.hengguan.base.contest.model.contest.SignGroupModel;
import org.hengguan.base.contest.service.contest.SignGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SignInService {

    @Autowired
    private SignGroupService signGroupService;

     public void groupLeader(SignGroupModel signGroupModel){
             SignGroupModel.builder()
                     .id(signGroupModel.getId())
                     .groupLeader(signGroupModel.getId())
                     .leaderName(signGroupModel.getPlName())
                     .build().updateById();
     }

    @Transactional
    public void randomGrouping(){

        SignGroupModel memberModel = signGroupService.getOne(new QueryWrapper<SignGroupModel>()
                .eq("is_seed", 0)
                .isNull("group_leader")
                .orderByAsc("RAND()")
                .last("LIMIT 1")
        );
        SignGroupModel leaderModel = signGroupService.getOne(new QueryWrapper<SignGroupModel>()
                .in("is_seed",1,2)
                .orderByAsc("RAND()")
                .last("LIMIT 1")
        );
        SignGroupModel.builder()
                        .id(memberModel.getId())
                        .groupLeader(leaderModel.getId())
                        .leaderName(leaderModel.getPlName())
                        .build().updateById();
        SignGroupModel.builder()
                .id(leaderModel.getId())
                .isSeed(leaderModel.getIsSeed()+1)
                .build().updateById();

    }
}
